import { objDeepCopy,getDateString } from "@/assets/js/common.js";

let Controller = {
	created() {
		this.init();
	},
	mounted() {
		
	},
	methods: {
		init() {
			this.getAllList(this,this.$route.query.number);
		},
		//
		onDialogEdit() {
			
		},
		//
		toggleTableDetail() {
			
		},
		//获取列表
		getAllList(that,orderNumber){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouseDetail/outAndInDetails',
					data:{
						condition:{
							number:orderNumber
						},
					}
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						that.Model.tab1TableData = [];
						var result = data.result;
						if(result){
							result.status == '待入库' ? that.Model.refundStep.activeStep = 1 : that.Model.refundStep.activeStep = 2;
							that.Model.tabFormInline = [
								{
									label:'调拨入库单',
									value:result.number
								},
								{
									label:'入库单状态',
									value:result.status
								}
							]
							that.Model.tableFormInline = [
								{
									label:'入库单类型',
									value:result.type
								},
								{
									label:'调拨出库单号',
									value:result.originNumber
								},
								{
									label:'发货仓库',
									value:result.outWarehouseName
								},
								{
									label:'收货仓库',
									value:result.inWarehouseName
								},
								{
									label:'操作人',
									value:result.status
								}
							]
							var obj = {}
							var dataInfo = result.shouldSaleGoodsInfoVos;
							if(dataInfo){
								for (let i = 0; i < dataInfo.length; i++) {
									obj = objDeepCopy(dataInfo[i]);
									that.Model.tab1TableData.push(obj);
								}
							}
								
							
						}
						
					}else{
						
					}
			  });
		},
		
		//
		//点击取消 返回上一级
		cancelOrder(){
			this.$router.push({
				path:'insideAllocation',
				query:{
					
				}
			});
		},
		//
		//扫码入库 请求
		codeEntryAjax(that,code){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'get',
					url:apiTest+'/supplier/whWarehouseDetail/getGoodsInfo?batchNo='+code,
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var obj = {};
						var result = data.result;
						if(result){
							obj = objDeepCopy(result);
							obj.mainQuantity = 1;
							if(result.batchNo){
								obj.isBatchNo = true;
							}else{
								obj.isBatchNo = false;
							}
							that.Model.tab1TableData2.push(obj)
						}
					}else{
			
					}
			
				});
		},
		//扫描条码
		codeEntry(){
			var inputCode = this.Model.inputCode;
			this.codeEntryAjax(this,inputCode);
		},
		//点击 删除添加的
		deleteTable(){
			var that = this;
			var TableSelectData = this.Model.TableSelectData;
			var tab1TableData2 = this.Model.tab1TableData2;
			if(TableSelectData.length == 0){
				this.tips(this,'warning','请先勾选需要删除的数据');
				return;
			}
			this.$confirm('此操作将删除相应数据, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				TableSelectData.forEach(function(value,index,array){
					tab1TableData2.forEach(function(val,idx,arr){
						if(value == val){
							that.Model.tab1TableData2.splice(idx,1);
						}
					})
				})
				this.$message({
					type: 'success',
					message: '删除成功!'
				});
			}).catch(() => {
			           
			});
		},
		//点击 提交调拨单
		onConfirmNew(){
			var arr = [];
			var obj = {};
			var number = this.$route.query.number;
			var tab1TableData2 = this.Model.tab1TableData2;
			for (let i = 0; i < tab1TableData2.length; i++) {
				obj = {
					sourceDetailsId:tab1TableData2[i].id,
					barCode:tab1TableData2[i].batchNo,
					mainQuantity:tab1TableData2[i].mainQuantity,
				}
				arr.push(obj);
			}
			if(tab1TableData2.length == 0){
				this.tips(this,'warning','请选择数据');
				return;
			}
			var params ={
				number:number,
				transferType:20,
				wDetails:arr
			}
			this.$confirm('此操作将确定入库订单, 是否继续?', '提示', {
			  confirmButtonText: '确定',
			  cancelButtonText: '取消',
			}).then(() => {
				this.confirmNewAjax(this,params);
			}).catch(() => {
			           
			});
			
		},
		//提交调拨单 请求
		confirmNewAjax(that,params){
			var apiTest = this.apiTest;
			this.$ajax({
					method: 'post',
					url:apiTest+'/supplier/whWarehouseDetail/operatingOutAndIn',
					data:params
			    }).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						var result = data.result;
						if(result == 1){
							that.tips(that,'success','操作成功');
							that.cancelOrder();
						}
					}else{
						that.tips(that,'warning',data.msg)
					}
			
				});
		},
		//返回上一级
		toInside() {
			this.$router.push({
				path:'insideAllocation',
				query:{
					
				}
			});
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		onDialogEdit(flag, val) {
// 			this.Model.editDialogFlag = flag;
// 			var saleId = this.Model.saleId;	
// 			if (flag === false) return;
// 			const arr = this.Model[val];
// 			this.Model.editDialogType = val;
// 			this.Model.editDialogFormData = _.cloneDeep(arr);
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		//合计
		getSummaries(param) {
			var that = this;
			const { columns, data } = param;
			const sums = [];
			columns.forEach((column, index) => {
		    if (index === 0) {
		      sums[index] = '总价';
		      return;
		    }else if(index === 6 || index === 7 || index === 8 || index === 10) {
					  const values = data.map(item => Number(item[column.property]));
					  sums[index] = values.reduce((prev, curr) => {
					    const value = Number(curr);
					    if (!isNaN(value)) {
					      return prev + curr;
					    } else {
					      return prev;
					    }
					  }, 0);
					  if(index === 10){
						that.Model.totleGoodCount = sums[index];
					  }else{
						
					  }
					  
				  }else{
					sums[index] = '/';
				  }
		  });
		
		  return sums;
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = val;
		},
		toSearch(val) {
			console.log(val)
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
		  console.log(val);
		},
		onTabChange(val) {
			console.log(val, this.Model.activeTab)
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		tips(that, type, message) {
			let duration;
			type == 'success' ? duration = 3000 : duration = 10000;
			that.$message({
				message: message,
				type: type,
				duration: duration
			});
		},
	},
	watch:{
		$route:{
		　　　handler(newValue, oldValue) {
				if(newValue.query.number) {
					this.getAllList(this,newValue.query.number);
					this.Model.inputCode = '';
					this.Model.tab1TableData2 = [];
				}
		　　　},
		　　　deep: true
		},
  	
	},
	filters:{
		objFilters:function(value){
			
		}
	}
}
export default Controller;
